package org.glut.cloud.repairManage.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.glut.cloud.repairManage.dto.RepairPeopleStateDto;
import org.glut.cloud.repairManage.service.IRepairPersonnelService;
import org.glut.cloud.repairManage.entity.RepairPersonnel;
import org.glut.cloud.repairManage.mapper.RepairPersonnelMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 维修人员表 服务实现类
 *
 * @author linghuhuolian
 * @since 2024-08-13 15:47
 */
@Service
public class RepairPersonnelServiceImpl extends ServiceImpl<RepairPersonnelMapper, RepairPersonnel> implements IRepairPersonnelService {

    @Resource
    private RepairPersonnelMapper repairPersonnelMapper;


    @Override
    public IPage page2(Page<RepairPersonnel> page, Wrapper wrapper) {
        return repairPersonnelMapper.page2(page,wrapper) ;
    }

    @Override
    public Integer getRepairPersonnelId(String username) {
        QueryWrapper<RepairPersonnel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        RepairPersonnel repairPersonnel = getOne(queryWrapper);

        if (repairPersonnel == null) {
            // 可以抛出自定义异常、返回默认值或日志记录等
            return null; // 或者 throw new MyException("Repair personnel not found");
        }

        return repairPersonnel.getRepairPersonnelId();
    }

    /**
     *  假删除
     * @param ids
     * @return
     */
    @Override
    public boolean fakeRemoveByIds(List<Integer> ids) {
        List<RepairPersonnel> personnelList = listByIds(ids);
        personnelList.forEach(personnel -> personnel.setIsDelete(1));
        return updateBatchById(personnelList);
    }


    /**
     *  获取维修人员状态
     * @param id
     * @return
     */

    @Override
    public RepairPeopleStateDto getRepairPersonnelState(Integer id) {
        RepairPersonnel repairPersonnel = getById(id);
        if (repairPersonnel != null && repairPersonnel.getIsDelete() == 0) {
            RepairPeopleStateDto stateDto = new RepairPeopleStateDto();
            stateDto.setRepairPersonnelId(repairPersonnel.getRepairPersonnelId());
            stateDto.setFullName(repairPersonnel.getFullName());
            stateDto.setMonthlyTaskCount(repairPersonnel.getMonthlyTaskCount());
            stateDto.setMonthlyTaskTarget(repairPersonnel.getMonthlyTaskTarget());
            stateDto.setTotalTaskCount(repairPersonnel.getTotalTaskCount());
            stateDto.setBusyStatus(repairPersonnel.getBusyStatus());
            return stateDto;
        }
        return null;
    }


}
